package com.ny.view;

import com.ny.dao.OrderDao;
import com.ny.dao.impl.OrderDaoImpl;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.ProductService;
import com.ny.service.impl.ProductServiceImpl;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class IndexView {
    private ProductService productService = new ProductServiceImpl();
    private OrderDao orderService = new OrderDaoImpl();

    public IndexView() throws SQLException, ClassNotFoundException {
    }

    public void indexView(Student student) throws SQLException, ClassNotFoundException {
        System.out.println("请选择相应操作");
        System.out.println("1.个人信息界面");
        System.out.println("2.查看商品信息");
        System.out.println("3.查看订单信息");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                System.out.println("个人信息界面");
                System.out.println("用户名：" + student.getStudentName());
                System.out.println("手机号：" + student.getStudentPhone());
                System.out.println("密码：" + student.getStudentPassword());
                System.out.println("请选择操作");
                System.out.println("1.修改密码");
                System.out.println("2.返回");
                int choice1 = scanner.nextInt();
                switch (choice1) {
                    case 1:
                        System.out.println("修改密码");
                        System.out.println("请输入新密码");
                        String newPassword = scanner.next();
                        student.setStudentPassword(newPassword);
                        int i = productService.updateStudentPassword(student.getStudentId(), student.getStudentPassword());
                        if (i > 0) {
                            System.out.println("修改成功！");
                            break;
                        }else {
                            System.out.println("修改失败！");
                        }
                        break;
                    case  2:
                        System.out.println("返回");
                        break;
                }
                break;
            case 2:
                System.out.println("商品信息界面");
                List<Product> productList = productService.findAllProduct();
                System.out.println("商品序号" + "\t" + "商品名称" + "\t" + "商品价格" + "\t" + "商品库存" + "\t" + "商品类别" + "\t" + "商品描述");
                for (Product product : productList) {
                    System.out.print(product.getProduct_id());
                    System.out.print("\t" + "\t");
                    System.out.print(product.getName());
                    System.out.print("\t" + "\t");
                    System.out.print(product.getPrice());
                    System.out.print("\t" + "\t");
                    System.out.print(product.getStock());
                    System.out.print("\t" + "\t");
                    System.out.print(product.getCategory());
                    System.out.print("\t" + "\t");
                    System.out.println(product.getDescription());
                }
                System.out.println("请输入你要购买的商品序号");
                int productId = scanner.nextInt();
                System.out.println("请输入你要购买的商品数量");
                int productNum = scanner.nextInt();
                if (productId > 0) {
                    if (productNum <= productService.findAllProduct().get(productId - 1).getStock()) {
                        System.out.println("商品库存充足！");
                        double totalPrice = productService.findAllProduct().get(productId - 1).getPrice() * productNum;
                        int sid = student.getStudentId();
                        Order order = new Order();
                        order.setStudentId(sid);
                        order.setProductId(productId);
                        order.setNumber( productNum);
                        order.setTotalPrice(totalPrice);
                        System.out.println("购买成功！总价为：" + totalPrice);
                    } else {
                        System.out.println("商品库存不足！");
                    }
                }else {
                    System.out.println("请输入正确的商品id");
                }
                productService.updateProductStock(productId, productNum);
                System.out.println("商品库存剩余：" + productService.findAllProduct().get(productId - 1).getStock());

                break;
            case 3:
                System.out.println("订单信息界面");
                System.out.println("根据用户id查找订单");
                List<Order> orderList = orderService.findAllOrder();
                System.out.println("订单序号" + "\t" + "用户id" + "\t" + "商品id" + "\t" + "商品数量" + "\t" + "总价");
                for (Order order1 : orderList){
                    System.out.print(order1.getOrderId());
                    System.out.print("\t" + "\t");
                    System.out.print(order1.getStudentId());
                    System.out.print("\t" + "\t");
                    System.out.print(order1.getProductId());
                    System.out.print("\t" + "\t");
                    System.out.print(order1.getNumber());
                    System.out.print("\t" + "\t");
                    System.out.println(order1.getTotalPrice());
                }

                break;
        }


    }
}
